//
//  LZMyChangeMobileView.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/10/28.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyChangeMobileView.h"
#import "LZMyTableViewButtonView.h"

#import "Masonry.h"
#import "BlocksKit+UIKit.h"

@interface LZMyChangeMobileView () <UITextFieldDelegate>
@property (nonatomic, assign) BOOL b_mobile_TF_Value;
@property (nonatomic, assign) BOOL b_verifi_TF_Value;

@property (nonatomic, strong) UIButton *areaCodeBtn;

@end

@implementation LZMyChangeMobileView

- (instancetype)initWithType:(LZ_CHANGEMOBILE_TYPE)type {
    
    self = [super init];
    if (self) {
        
        self.backgroundColor = UIColorFromRGB(0xf9f7f7, 1);
        
        self.type = type;
        
        if (self.type == LZ_CHANGEMOBLIE_TYPE_IDCHECK) {
            [self configView];
            [self addAutoLayout];
        }
        else {
            [self configViewLinkNew];
            [self addAutoLayoutLinkNew];
        }
    }
    
    return self;
}

- (void)configView {
    
    [self addSubview:self.titleLabel];
    [self addSubview:self.contentView];
    
    [self.contentView addSubview:self.areaCodeTextField];
    [self.contentView addSubview:self.areaCodeBtn];
    [self.contentView addSubview:self.markImageView];
    [self.contentView addSubview:self.mobileNumTextField];
    [self.contentView addSubview:self.verificCodeButton];
    [self.contentView addSubview:self.countdownLabel];
    [self.contentView addSubview:self.line1];
    [self.contentView addSubview:self.verificTitleLabel];
    [self.contentView addSubview:self.verificCodeTextField];
    
    [self addSubview:self.subLabel];
    [self addSubview:self.confirmButton];
}

- (void)addAutoLayout {
    
    WS(weakSelf);
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(15);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(10);
        make.height.mas_equalTo(15);
    }];
    
    [self.contentView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(15);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(108);
    }];
    
    [self.subLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(10);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(weakSelf.confirmButton.mas_top).with.offset(-40);
    }];
    
    [self.confirmButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.subLabel.mas_bottom).with.offset(40);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(40);
    }];
    
    [self.areaCodeTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(54, 54));
    }];
    
    [self.areaCodeBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(54, 54));
    }];
    
    [self.markImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.areaCodeTextField.mas_centerY);
        make.leading.mas_equalTo(weakSelf.areaCodeTextField.mas_trailing);
        make.size.mas_equalTo(CGSizeMake(6.5, 13));
    }];
    
    [self.mobileNumTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(weakSelf.areaCodeTextField.mas_trailing).with.offset(20);
        make.trailing.mas_equalTo(weakSelf.verificCodeButton.mas_leading).with.offset(-10);
        make.height.mas_equalTo(54);
    }];
    
    [self.verificCodeButton makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-10);
        make.centerY.mas_equalTo(weakSelf.areaCodeTextField.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(32, 19));
    }];
    
    [self.countdownLabel makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-10);
        make.centerY.mas_equalTo(weakSelf.areaCodeTextField.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(32, 19));
    }];
    
    [self.line1 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.areaCodeTextField.mas_bottom);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(1);
    }];
    
    [self.verificTitleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.line1.mas_bottom).with.offset(20);
        make.leading.mas_equalTo(10);
        make.width.mas_equalTo(SCREEN_WIDTH/3.0);
    }];
    
    [self.verificCodeTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.line1.mas_bottom);
        make.leading.mas_equalTo(weakSelf.verificTitleLabel.mas_trailing).with.offset(20);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)configViewLinkNew {
    
//    [self addSubview:self.titleLabel];
    [self addSubview:self.contentView];
    
    [self.contentView addSubview:self.areaCodeTextField];
    [self.contentView addSubview:self.areaCodeBtn];
    [self.contentView addSubview:self.markImageView];
    [self.contentView addSubview:self.mobileNumTextField];
    [self.contentView addSubview:self.verificCodeButton];
    [self.contentView addSubview:self.countdownLabel];
    [self.contentView addSubview:self.line1];
    [self.contentView addSubview:self.verificTitleLabel];
    [self.contentView addSubview:self.verificCodeTextField];
    
    [self addSubview:self.confirmButton];
}

- (void)addAutoLayoutLinkNew {
    
    WS(weakSelf);
    
//    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(15);
//        make.leading.mas_equalTo(10);
//        make.trailing.mas_equalTo(10);
//        make.height.mas_equalTo(15);
//    }];
    
    [self.contentView makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(weakSelf.titleLabel.mas_bottom).with.offset(15);
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.height.mas_equalTo(108);
    }];
    
    [self.confirmButton makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.contentView.mas_bottom).with.offset(40);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(40);
    }];
    
    [self.areaCodeTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(54, 54));
    }];
    
    [self.areaCodeBtn makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(54, 54));
    }];
    
    [self.markImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.areaCodeTextField.mas_centerY);
        make.leading.mas_equalTo(weakSelf.areaCodeTextField.mas_trailing);
        make.size.mas_equalTo(CGSizeMake(6.5, 13));
    }];
    
    [self.mobileNumTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.leading.mas_equalTo(weakSelf.areaCodeTextField.mas_trailing).with.offset(20);
        make.trailing.mas_equalTo(weakSelf.verificCodeButton.mas_leading).with.offset(-10);
        make.height.mas_equalTo(54);
    }];
    
    [self.verificCodeButton makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-6);
        make.centerY.mas_equalTo(weakSelf.areaCodeTextField.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [self.countdownLabel makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(-10);
        make.centerY.mas_equalTo(weakSelf.areaCodeTextField.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(32, 19));
    }];
    
    [self.line1 makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.areaCodeTextField.mas_bottom);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(1);
    }];
    
    [self.verificTitleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.line1.mas_bottom).with.offset(20);
        make.leading.mas_equalTo(10);
        make.width.mas_equalTo(SCREEN_WIDTH/3.0);
    }];
    
    [self.verificCodeTextField makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.line1.mas_bottom);
        make.leading.mas_equalTo(weakSelf.verificTitleLabel.mas_trailing).with.offset(20);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark - btn action
- (void)openAreaCodeVC
{
    if (self.openAreaCodeVCBlock) {
        self.openAreaCodeVCBlock(self.areaCodeTextField);
    }
}

#pragma mark - UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.tag == 100) {
        NSInteger strLength = textField.text.length - range.length + string.length;
        if (strLength < 1 || strLength > 5) {
            return NO;
        }
    }
    else if (textField.tag == 200) {
        
        NSInteger strLength = textField.text.length - range.length + string.length;
        
        if (strLength > 0) {
            [_verificCodeButton setImage:[UIImage imageNamed:@"lz_getverification_icon_hd"] forState:UIControlStateNormal];
            self.b_mobile_TF_Value = YES;
        } else {
            [_verificCodeButton setImage:[UIImage imageNamed:@"lz_getverification_icon"] forState:UIControlStateNormal];
            self.b_mobile_TF_Value = NO;
        }
        
        if (strLength > 20) {
            return NO;
        }
    }
    else if (textField.tag == 300) {
        NSInteger strLength = textField.text.length - range.length + string.length;
        
        if (strLength > 0) {
            self.b_verifi_TF_Value = YES;
        } else {
            self.b_verifi_TF_Value = NO;
        }
    }
    
    if (self.b_mobile_TF_Value && self.b_verifi_TF_Value) {
        self.confirmButton.enabled = YES;
        [self.confirmButton setBackgroundColor:UIColorFromRGB(0xf2871c, 1)];
    }
    else {
        self.confirmButton.enabled = NO;
        [self.confirmButton setBackgroundColor:UIColorFromRGB(0xcccccc, 1)];
    }
    
    return YES;
}

#pragma mark - Setter/Getter

- (UILabel *)titleLabel{
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        
        if (self.type == LZ_CHANGEMOBLIE_TYPE_IDCHECK) {
            _titleLabel.text        = LZLocalizedString(@"change_idcheck", @"");
        } else {
            _titleLabel.text        = LZLocalizedString(@"change_link_newmobile", @"");
        }
        _titleLabel.textColor   = UIColorFromRGB(0x999999, 1);
        _titleLabel.font        = [UIFont systemFontOfSize:14];
    }
    
    return _titleLabel;
}

- (UIView *)contentView {
    
    if (_contentView == nil) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = UIColorFromRGB(0xffffff, 1);
    }
    
    return _contentView;
}

- (UITextField *)areaCodeTextField {
    
    if (_areaCodeTextField == nil) {
        _areaCodeTextField = [[UITextField alloc] init];
        _areaCodeTextField.text             = @"+973";
        _areaCodeTextField.textAlignment    = NSTextAlignmentLeft;
        _areaCodeTextField.font             = [UIFont systemFontOfSize:15.0];
        _areaCodeTextField.tag              = 100;
        _areaCodeTextField.delegate         = self;
        _areaCodeTextField.keyboardType     = UIKeyboardTypeNumberPad;
        [_areaCodeTextField setTextColor:UIColorFromRGB(0x333333, 1)];
        [_areaCodeTextField setTintColor:UIColorFromRGB(0x33a928, 1)];
    }
    
    return _areaCodeTextField;
}

- (UIButton *)areaCodeBtn
{
    if (!_areaCodeBtn) {
        _areaCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _areaCodeBtn.backgroundColor = [UIColor clearColor];
        [_areaCodeBtn addTarget:self action:@selector(openAreaCodeVC) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _areaCodeBtn;
}

- (UIImageView *)markImageView {
    
    if (_markImageView == nil) {
        _markImageView = [[UIImageView alloc] init];
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            _markImageView.image = [UIImage imageNamed:@"lz_myinfo_mobilearea_image_ar"];
        } else {
            _markImageView.image = [UIImage imageNamed:@"lz_myinfo_mobilearea_image_en"];
        }
    }
    
    return _markImageView;
}

- (UITextField *)mobileNumTextField {
    
    if (_mobileNumTextField == nil) {
        _mobileNumTextField = [[UITextField alloc] init];
        if (self.type == LZ_CHANGEMOBLIE_TYPE_IDCHECK) {
            _mobileNumTextField.placeholder = LZLocalizedString(@"change_enter_current_mobile_number", @"");
        } else {
            _mobileNumTextField.placeholder = @"Please enter your mobile phone";//LZLocalizedString(@"login_enter_mobile", @"");
        }
        _mobileNumTextField.font            = [UIFont systemFontOfSize:14];
        _mobileNumTextField.tag             = 200;
        _mobileNumTextField.delegate        = self;
        _mobileNumTextField.keyboardType    = UIKeyboardTypePhonePad;
        [_mobileNumTextField setTextColor:UIColorFromRGB(0x666666, 1)];
        [_mobileNumTextField setValue:UIColorFromRGB(0xcccccc, 1) forKeyPath:@"_placeholderLabel.textColor"];
        [_mobileNumTextField setTintColor:UIColorFromRGB(0x33a928, 1)];
    }
    
    return _mobileNumTextField;
}

- (UIButton *)verificCodeButton {
    
    WS(weakSelf);
    if (_verificCodeButton == nil) {
        _verificCodeButton = [[UIButton alloc] init];
        [_verificCodeButton setImage:[UIImage imageNamed:@"lz_getverification_icon"] forState:UIControlStateNormal];
        [_verificCodeButton setImage:[UIImage imageNamed:@"lz_getverification_icon_hd"] forState:UIControlStateSelected];
        [_verificCodeButton bk_addEventHandler:^(UIButton * sender) {
            
            if (weakSelf.getVerificButtonBlock) {
                weakSelf.getVerificButtonBlock(sender);
            }
            
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _verificCodeButton;
}

- (UILabel *)countdownLabel {
    
    if (_countdownLabel == nil) {
        _countdownLabel = [[UILabel alloc] init];
        _countdownLabel.textColor           = UIColorFromRGB(0xcccccc, 1);
        _countdownLabel.text                = @"60s";
        _countdownLabel.font                = [UIFont systemFontOfSize:14];
        _countdownLabel.layer.masksToBounds = YES;
        _countdownLabel.layer.cornerRadius  = 2.5;
        _countdownLabel.layer.borderWidth   = 1;
        _countdownLabel.layer.borderColor   = UIColorFromRGB(0xcccccc, 1).CGColor;
        _countdownLabel.hidden              = YES;
        _countdownLabel.textAlignment       = NSTextAlignmentCenter;
    }
    
    return _countdownLabel;
}

- (UIView *)line1 {
    
    if (_line1 == nil) {
        _line1 = [[UIView alloc] init];
        _line1.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line1;
}

- (UILabel *)verificTitleLabel {
    
    if (_verificTitleLabel == nil) {
        
        _verificTitleLabel = [[UILabel alloc] init];
        _verificTitleLabel.text         = LZLocalizedString(@"change_verificode", nil);
        _verificTitleLabel.font         = [UIFont systemFontOfSize:15.0];
        _verificTitleLabel.textColor    = UIColorFromRGB(0x333333, 1);
    }
    
    return _verificTitleLabel;
}

- (UITextField *)verificCodeTextField {
    
    if (_verificCodeTextField == nil) {
        _verificCodeTextField = [[UITextField alloc] init];
        _verificCodeTextField.placeholder   = LZLocalizedString(@"change_verificode", nil);
        _verificCodeTextField.font          = [UIFont systemFontOfSize:14.0];
        _verificCodeTextField.delegate      = self;
        _verificCodeTextField.tag           = 300;
        _verificCodeTextField.keyboardType  = UIKeyboardTypeNumberPad;
        [_verificCodeTextField setTextColor:UIColorFromRGB(0x666666, 1)];
        [_verificCodeTextField setValue:UIColorFromRGB(0xcccccc, 1) forKeyPath:@"_placeholderLabel.textColor"];
        [_verificCodeTextField setTintColor:UIColorFromRGB(0x33a928, 1)];
    }
    
    return _verificCodeTextField;
}

- (UILabel *)subLabel {
    
    if (_subLabel == nil) {
        _subLabel = [[UILabel alloc] init];
        _subLabel.textColor     = UIColorFromRGB(0xbababa, 1);
        _subLabel.font          = [UIFont systemFontOfSize:12];
        _subLabel.numberOfLines = 0;
        
        NSString * str = LZLocalizedString(@"change_tip_message", nil);
        NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle * paragraphStyle1 = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle1 setLineSpacing:5];
        [attributedString1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle1 range:NSMakeRange(0, [str length])];
        [_subLabel setAttributedText:attributedString1];
        [_subLabel sizeToFit];
    }
    
    return _subLabel;
}

- (UIButton *)confirmButton {
    
    WS(weakSelf);
    if (_confirmButton == nil) {
        _confirmButton = [[UIButton alloc] init];
        _confirmButton.backgroundColor      = UIColorFromRGB(0xcccccc, 1);
        _confirmButton.layer.masksToBounds  = YES;
        _confirmButton.layer.cornerRadius   = 5.0;
        _confirmButton.enabled              = NO;
        if (self.type == LZ_CHANGEMOBLIE_TYPE_IDCHECK) {
            [_confirmButton setTitle:LZLocalizedString(@"nextstep", nil) forState:UIControlStateNormal];
        } else {
            [_confirmButton setTitle:LZLocalizedString(@"confirm", nil) forState:UIControlStateNormal];
        }
        [_confirmButton setTitleColor:UIColorFromRGB(0xffffff, 1) forState:UIControlStateNormal];
        [_confirmButton bk_addEventHandler:^(UIButton * sender) {
            
            if (weakSelf.nextStepButtonBlock) {
                weakSelf.nextStepButtonBlock(sender);
            }
            
        } forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _confirmButton;
}

@end
